//获取当前定位

// #ifdef APP-PLUS
import permision from "@/common/utils/permission.js";
// #endif
// #ifndef APP-PLUS
import amap from "@/common/libs/gaode/amap-wx.js";
// #endif

export async function getLocation() {
  //首先检查权限
  // #ifdef APP-PLUS
  //安卓检查权限有问题，暂时先不检查权限了
  // let status = await checkPermission();
  // console.log('status', status)
  // if (status !== 1) {
  //     return;
  // }
  // #endif
  // #ifdef MP-WEIXIN || MP-TOUTIAO || MP-QQ || MP-ALIPAY
  let status = await getSetting();
  if (status === 2) {
    uni.showModal({
      content: "上铁12306请求访问您的位置信息",
      confirmText: "设置",
      success: function (res) {
        if (res.confirm) {
          openSetting();
        }
      },
    });
    return;
  }
  // #endif

  //然后发起定位
  return new Promise((resolve, reject) => {
    console.log("发起定位");
    // #ifdef APP-PLUS
    console.log("app端截取成功");
    uni.getLocation({
      type: "gcj02",  //gcj02 wgs84
      geocode: true,
      success: (res) => {
			console.log("APP定位成功", res);
			var address = res.address
			address.latitude = res.latitude
			address.longitude = res.longitude
			resolve({code: 200, data: address});
      },
      fail: (err) => {
        console.log("APP定位失败", err);
        reject(err);
      },
    });
    // #endif
    // #ifndef APP-PLUS
    //初始化高德SDK对象
    var amapPlugin = new amap.AMapWX({
      key: "6cd8d1cbeda1fcd4053da32267e465b3",
    });
    amapPlugin.getRegeo({
      success: (data) => {
        var city = data[0].regeocodeData.addressComponent.city;
        if (!city || city.length < 1) {
          city = data[0].regeocodeData.addressComponent.province;
        }
        console.log("小程序定位成功", city);
        var address = data[0];
        address.city = city;
        resolve(address);
      },
      fail: (err) => {
        console.log("小程序定位失败", err);
        reject(err);
      },
    });
    // #endif
  });
}

function getSetting() {
  return new Promise((resolve, reject) => {
    uni.getSetting({
      success: (res) => {
        if (res.authSetting["scope.userLocation"] === undefined) {
          resolve(0);
          return;
        }
        if (res.authSetting["scope.userLocation"]) {
          resolve(1);
        } else {
          resolve(2);
        }
      },
    });
  });
}
function openSetting() {
  // this.hideConfirm();
  uni.openSetting({
    success: (res) => {
      if (res.authSetting && res.authSetting["scope.userLocation"]) {
      }
    },
    fail: (err) => {},
  });
}
async function checkPermission() {
  console.log("开始检查权限");
  let status = permision.isIOS ? await permision.requestIOS("location") : 1; //ios才做权限判断，安卓有问题，不在这里做权限判断
  console.log("权限status", status);
  if (status === null || status === 1) {
    status = 1;
  } else if (status === 2) {
    uni.showModal({
      content: "系统定位已关闭",
      confirmText: "确定",
      showCancel: false,
      success: function (res) {},
    });
  } else if (status.code) {
    uni.showModal({
      content: status.message,
    });
  } else {
    uni.showModal({
      content: "需要定位权限",
      confirmText: "设置",
      success: function (res) {
        if (res.confirm) {
          permision.gotoAppSetting();
        }
      },
    });
  }

  return status;
}
